package icu.lookyousmileface.lockeight;

import java.util.concurrent.TimeUnit;

/**
 * @author starrysky
 * @title: LockProblem1
 * @projectName Juc_Pro
 * @description: 同一个对象同一把锁，谁拿到谁先执行！非顺序执行！
 * @date 2021/1/2911:07 上午
 */
public class LockProblem1 {
    public static void main(String[] args) {

        Phone phone1 = new Phone();

        new Thread(()->{phone1.sendSMS();}).start();
        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        new Thread(()->{phone1.call();}).start();
    }
}

/**
 * 资源类
 */
class Phone {
    public synchronized void sendSMS() {
        try {
            TimeUnit.SECONDS.sleep(4);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("发送短信");
    }

    public synchronized void call(){
        System.out.println("打电话");
    }
}